package com.ang.juc.n4.t1;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * ReentrantLock
 */
@Slf4j
public class Test2 {

    static ReentrantLock lock = new ReentrantLock();
    static Condition c1 = lock.newCondition();
    static Condition c2 = lock.newCondition();


    public static void main(String[] args) {

        Thread t1 = new Thread(() -> {
            try {
                //获得锁之后才能await
                lock.lock();
                c1.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                lock.unlock();
            }
            log.info("t1运行");
        }, "t1");

        Thread t2 = new Thread(() -> {
            try {
                lock.lock();
                log.info("t2运行");

                //唤醒t1
                c1.signal();
            } finally {
                lock.unlock();
            }
        }, "t2");

        t1.start();
        t2.start();
    }
}
